<?php

namespace App\Service\Model;

use App\User;
class Cards extends \Eloquent
{
    const attribute = [
            'cardno'    => '卡号',
            'acctname'  => '姓名',
            'accttype'  => '卡类型',
            'smscode'   => '短信验证码',
            'payDate'   => '还款日',
            'billDate'  => '账单日',
            'bankName'  => '银行名称',
    ];

    const status_default   = 1;//初始状态
    const status_unconfirm = 2;//已发送验证码
    const status_success   = 3;//确认绑卡成功
    const status_delete    = 4;//已解绑

    const plan_status_off  = 0;//没有计划在执行
    const plan_status_on   = 1;//计划正在执行
    
    const status_text = [
        self::status_default   => '初始状态',
        self::status_unconfirm => '等待验证',
        self::status_success   => '绑定成功',
        self::status_delete    => '已解绑'
    ];

    //const status_success   = 0000;
    //const status_confirm   = 1999;

    const debit_card  = '00';//借记卡
    const credit_card = '02';//信用卡

    /**
     * The database used by the model.
     *
     * @var string
     */
    protected $table = 'cards';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['agency_id','bank_name','bill_date','pay_date','uid','cardno','acctname','accttype','validdate','cvv2','idno','tel','agreeid','memid','thpinfo','ret','cusid'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [''];



    public function __construct(array $attributes = []){
        parent::__construct($attributes);
    }

    /**
     * 处理json数据
     *
     * @param string $val
     * @return string
     */
    public function getRetAttribute($val){
        if(is_null($val)){
            return '无返回值';
        }
        return json_encode(json_decode($val),JSON_UNESCAPED_UNICODE);
    }

    


    /**
     * 获取卡
     *
     * @param [type] $cardno
     * @return void
     */
    public static function getCardByNo($cardno){
        return self::where('cardno',$cardno)->where('is_delete',0)->first();
    }
    /**
     * 是否可以进行绑定
     *
     * @param [type] $cardno
     * @return boolean
     */
    public static function canBind($cardno){
        $card       =   self::getCardByNo($cardno);
        if(!empty($card) && $card->trxstatus == self::status_success){
            return false;
        }
        return true;
    }

    /**
     * 判断卡的状态
     *
     * @param [type] $cardno
     * @return boolean
     */
    public static function canCreateOrder($cardno){
      
    }

  

    /**
     * 获取用户信息
     *
     * @return void
     */
    public function user(){
        return $this->belongsTo(User::class,'uid','id');
    }
}
